package com.assets.menu;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.assets.Fragment.Query;
import com.assets.newNHF.util.LogUtils;
import com.assets.util.MyOkHttp;
import com.assets.util.RequestResult;
import com.assets.util.Since_case;
import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnDismissListener;
import com.assets.Fragment.Allot;
import com.assets.Fragment.Fragment1;
import com.assets.Fragment.Fragment2;
import com.assets.Fragment.Fragment3;
import com.assets.Fragment.Fragment4;
import com.assets.Fragment.Fragment5;
import com.assets.Fragment.Fragment6;
import com.assets.Fragment.Fragment7;
import com.assets.Fragment.Maintain;
import com.assets.Fragment.Maintenance_service;
import com.assets.Fragment.Polling;
import com.assets.Fragment.Repairs;
import com.assets.Fragment.Scrap;
import com.assets.R;
//import com.gg.reader.api.utils.HksPower;
import com.lemonade.widgets.slidesidemenu.SlideSideMenuTransitionLayout;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Response;

public class SlideSideMenuExampleActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, OnDismissListener {

    private Dialog dialog;
    // 列表数据
    private LinearLayout home;
    private ArrayList<Group> gData = null;
    private ArrayList<ArrayList<Item>> iData = null;
    private ExpandableListView exlist_lol;
    private MyBaseExpandableListAdapter myAdapter = null;
    private SlideSideMenuTransitionLayout mSlideSideMenu;
    private long mExitTime;
    private FragmentManager fragmentManager;
    private Fragment fragment1, fragment2, fragment3, fragment4, fragment5, fragment6, fragment7, Allot, Scrap, Repairs, Polling, Maintain, Maintenance_service, query;
    private Map<Integer, Fragment> mapFragment = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slide_side_menu_example);
        in();
/*
Fragment1-首页
Fragment2-入库
Fragment3-领用
Fragment4-盘点
Fragment5-租赁
Fragment6-归还
Fragment7-转移
Allot-调拨
Maintain-设备管理
Polling-巡检
Repairs-报修
Scrap-报废
        // Setup the toolbar
        mToolbar = (Toolbar) findViewById(R.id.base_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setTitleTextColor(Color.WHITE);

        // Wire SideMenu with Toolbar
        mToolbar.setNavigationIcon(R.drawable.ic_menu);

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSlideSideMenu.toggle();
            }
        });*/
    }

    private void in() {
//        HksPower.uhf_power(1);//上电
        // polling_da = (LinearLayout) findViewById(R.id.polling);
        home = (LinearLayout) findViewById(R.id.home);
//        fragment1 = new Fragment1();
//        fragment2 = new Fragment2();
//        fragment3 = new Fragment3();//领用
        fragment4 = new Fragment4();//盘点
//        fragment5 = new Fragment5();
//        fragment6 = new Fragment6();//归还
//        fragment7 = new Fragment7();//调拨
//        Allot = new Allot();
//        Scrap = new Scrap();//报废
//        Repairs = new Repairs();
//        Polling = new Polling();
//        query = new Query();
        Maintenance_service = new Maintenance_service();
        Maintain = new Maintain();

        mSlideSideMenu = (SlideSideMenuTransitionLayout) findViewById(R.id.slide_side_menu);
//        mSlideSideMenu.closeSideMenu();
        mSlideSideMenu.setLocked(true);
        //  switchFragment(fragment1);
        fragmentManager = getSupportFragmentManager();

        fragmentManager.beginTransaction()
                .replace(R.id.id_framelayout2, fragment4).commit();
        home.setOnClickListener(v -> {
            fragmentManager.beginTransaction()
                    .replace(R.id.id_framelayout2, fragment4).commit();
//            mSlideSideMenu.toggle();
        });
 /*       polling_da.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, Polling
                        ).commit();
                mSlideSideMenu.toggle();
            }
        });*/
        exlist_lol = (ExpandableListView) findViewById(R.id.expendlist);
        exlist_lol.setGroupIndicator(null);//数据准备
        gData = new ArrayList<Group>();
        iData = new ArrayList<ArrayList<Item>>();
        //固定资产组
        mapFragment.put(gData.size(), query);
        gData.add(new Group("查询", R.mipmap.search));
//        mapFragment.put(gData.size(), fragment2);
//        gData.add(new Group("入库", R.mipmap.in_storage));
//        mapFragment.put(gData.size(), fragment3);
//        gData.add(new Group("领用", R.mipmap.receive));
//        mapFragment.put(gData.size(), fragment5);
//        gData.add(new Group("租赁", R.mipmap.lease));
//        mapFragment.put(gData.size(), fragment6);
//        gData.add(new Group("归还", R.mipmap.huan));
        mapFragment.put(gData.size(), fragment4);
        gData.add(new Group("盘点", R.mipmap.check));
        mapFragment.put(gData.size(), fragment7);
        gData.add(new Group("调拨", R.mipmap.transfer));
//        mapFragment.put(gData.size(), Allot);
//        gData.add(new Group("转移", R.mipmap.allot));
        mapFragment.put(gData.size(), Scrap);
        gData.add(new Group("报废", R.mipmap.scrap));
//        mapFragment.put(gData.size(), Repairs);
//        gData.add(new Group("报修", R.mipmap.repairs));
        myAdapter = new MyBaseExpandableListAdapter(gData, iData, this);
        exlist_lol.setAdapter(myAdapter);
        exlist_lol.setOnGroupClickListener((parent, v, groupPosition, id) -> {
            fragmentManager.beginTransaction()
                    .replace(R.id.id_framelayout2, mapFragment.get(groupPosition)).commit();
            mSlideSideMenu.toggle();
            return true;
        });
        //为列表设置点击事件

    }

    @Override
    public void onBackPressed() {
        if (mSlideSideMenu != null && mSlideSideMenu.closeSideMenu()) {
            // Closed the side menu, override the default back pressed behavior
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
       /* switch (position) {
            case 0:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, fragment1).commit();
                break;
            case 1:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, fragment2).commit();
                break;
            case 2:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, fragment3).commit();
                break;
            case 3:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, fragment4).commit();
                break;
            case 4:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, fragment5).commit();
                break;
            case 5:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, fragment6).commit();
                break;
            case 6:
                if (Since_case.getInstance().getData().equals("1")) {
                    showToast("查看所属部门权限不可调拨");
                    return;
                }
                if (Since_case.getInstance().getData().equals("2")) {
                    showToast("查看所属事业部权限不可调拨");
                    return;
                }
                *//*if (!Since_case.getInstance().getData().equals("3") && !Since_case.getInstance().getData().equals("4")&& !Since_case.getInstance().getData().equals("0")) {
                    showToast("非管理部门无法使用调拨功能");
                    return;
                }*//*
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, fragment7).commit();
                break;
            case 7:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, Allot).commit();
                break;
            case 8:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, Scrap).commit();
                break;
            case 9:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, Repairs).commit();
                break;
        }*/
        mSlideSideMenu.toggle();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        //判断用户是否点击了“返回键”
      /*  if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            if ((System.currentTimeMillis() - mExitTime) > 500) {
                //大于2000ms则认为是误操作，使用Toast进行提示
//                Toast.makeText(this, "再按一次返回登陆界面", Toast.LENGTH_SHORT).show();

                AlertDialog.Builder builder = new AlertDialog.Builder(SlideSideMenuExampleActivity.this);
                LayoutInflater inflater = LayoutInflater.from(SlideSideMenuExampleActivity.this);
                View v = inflater.inflate(R.layout.layout_quit, null);

                @SuppressLint({"MissingInflatedId", "LocalSuppress"})
                Button btnOk = v.findViewById(R.id.download_btn_ok);
                Button btnCancel = v.findViewById(R.id.download_btn_cancel);

                dialog = builder.create();
                dialog.show();
                dialog.getWindow().setContentView(v);

                btnOk.setOnClickListener(view -> {

                    finish();

                });

                btnCancel.setOnClickListener(view -> {

                    dialog.dismiss();

                });

                //并记录下本次点击“返回键”的时刻，以便下次进行判断
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }*/
        return super.onKeyDown(keyCode, event);
    }

    public void opmenu() {
        mSlideSideMenu.toggle();
    }

    @Override
    public void onDismiss(Object o) {

    }

    private Toast toast;

    public void showToast(final String msg) {
        runOnUiThread(() -> {
            if (toast != null) {
                toast.cancel();
            }
            toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
            toast.show();
        });
    }

    //加载进度弹窗
    private volatile ProgressDialog progressDialog;

    public void showDialog(String title) {
        runOnUiThread(() -> {
            if (progressDialog == null) {
                progressDialog = new ProgressDialog(this);
            }
            progressDialog.setTitle(title);
            progressDialog.setMessage("Loading...");
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setCancelable(true);
            progressDialog.show();
        });
    }

    private AlertView alertView;

    public void showTipDialog(RequestResult<Boolean> result, String msg) {
        this.runOnUiThread(() -> {
            if (this.alertView == null || !this.alertView.isShowing()) {
                this.alertView = new AlertView("提示", msg, "取消", new String[]{"确认"}, (String[]) null, this, AlertView.Style.Alert, (o, position) -> {
                    this.alertView.dismissImmediately();
                    if (position == -1) {
                        result.result(false);
                    }
                    if (position == 0) {
                        result.result(true);
                    }
                });
                this.alertView.setCancelable(false);
                this.alertView.show();
            }
        });
    }


    public void canDialog() {
        runOnUiThread(() -> {
            if (progressDialog != null) {
                if (progressDialog.isShowing()) {
                    progressDialog.cancel();
                }
            }
        });
    }

    public void post(String url, Map<String, String> map, RequestResult<String> result) {
        MyOkHttp.getInstance().post(url, map, new MyOkHttp.requestMessage() {
            @Override
            public void requestFail() {
                showToast("连接服务器失败");
            }

            @Override
            public void requestComplete() {
                canDialog();
            }

            @Override
            public void requestSuccess(Response response) throws IOException {
                String responseData = response.body().string();
                result.result(responseData);
                //setData(queryData);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

//        HksPower.uhf_power(0);//上电
    }
}
